﻿#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

#define MaxWordsNum 1000 //最多有多少个单词
#define MaxWordLength 50 //单词的最大长度

void PrintWords(char Words[MaxWordsNum][MaxWordLength]); //打印WordsOriginal

char str[] = "The number of donors has been increasing rapidly since the system was launched in March 2014, said Zhao Hongtao, deputy secretary general of the foundation. In 2014, only 17552 people registered through the system, according to the foundation.\
The total number of registered organ donors in China exceeded 422000 by March 28, according to the latest figures released by the China Organ Donation Administrative Center, under the Red Cross Society of China.\
Only the two organizations - China Organ Transplantation Development Foundation and China Organ Donation Administrative Center - are eligible for donor registration in China. People can register through online platforms run by the two organizations, or contact local Red Cross societies.\
To facilitate registration, people can also complete the whole registration procedure on Alipay in as little as 10 seconds, Zhao said.\
The rapid increase in the number of organ donors in China shows great potential for organ donation and transplantation, said Huang Jiefu, chairman of the foundation.\
More similar innovative efforts are needed from various parties, including the Red Cross Society of China, to keep the number of organ donors rapidly increasing, he said.\
Every year more than 300,000 patients with terminal diseases wait for organ transplants in China, but only about 10000 transplants are carried out due to a lack of donated organs, according to the foundation.\
Guo Yanhong, deputy chief for medical administration and supervision at National Health Commission, said the number of people who have donated organs after death in China is expected to exceed 6000 by the end of year.\
Organ donation and transplantation has made rapid progress in China since 2015, the year China stopped transplant surgeries sourced from executed prisoners, making voluntary donation the only legitimate source, she said.\0";
//打印名字和学号的函数
void PrintInfo(void)
{
	printf("My Name is : %s\n", "ZHANG");
	printf("My Student ID is : %s\n", "22156558");
}


int main()
{
	PrintInfo();
	printf("\nThe original string is: %s\n", str);
	char WordsOriginal[MaxWordsNum][MaxWordLength]; //第一维（数组下标）为单词的序号，第二维保存单词
//	初始化
	for (int i = 0; i <= MaxWordsNum - 1; i++)
	{
		strcpy(WordsOriginal[i], "\0");
	}
	int i = 0, j = 0; //i,j用来作为二维数组Words的下标
	long long unsigned int k = 0; //用来从str中读入字符
	while (k < strlen(str))
	{
		char c = str[k];
		if (c == ' ' || c == ',' || c == '.' || c == '\n')
		{
			//读进来的字符不是空格，逗号，句号，回车
			i++;//准备将下一个字符作为新的单词
			j = 0;//新的单词开始位置
		}
		else
		{
			WordsOriginal[i][j] = c;
			j++;
		}
		k++;
	}

	printf("\n打印WordsOriginal\n");
	PrintWords(WordsOriginal);//打印WordsOriginal

	//	WordsOriginal里边存放的是原始单词，有可能有很多是重复的，现在要去除重复的
	int WordsNum[MaxWordsNum];//存放单词序号（数组下标）对应的单词出现次数
	char WordsDistinct[MaxWordsNum][MaxWordLength]; //第一维（数组下标）为单词的序号，第二维保存单词, 没有重复的单词
	//	初始化
	for (int a = 0; a <= MaxWordsNum - 1; a++)
	{
		WordsNum[a] = 0;
		strcpy(WordsDistinct[a], "\0");
	}
	int d = 0;//WordsDistinguished二维数组中的单词序号
	for (int i = 0; i <= MaxWordsNum - 1; i++)
	{
		char CurStr[MaxWordLength] = ""; //当前的单词
		strcpy(CurStr, WordsOriginal[i]); //copy string
		//如果是空格则跳过
		if (strcmp(CurStr, "") == 0 )
			continue;
		//和已有的进行比较
		int IsExisted = 0;//does it exist in the WordsDistinct array
		for (int j = 0; j <= MaxWordsNum - 1; j++)
		{
			if (strcmp(CurStr, WordsDistinct[j]) == 0 )
			{
				//相等，是已有的单词
				WordsNum[j]++;//词频数+1
				IsExisted = 1;
				break;
			}
		}
		if (IsExisted == 0)
		{
			strcpy(WordsDistinct[d], CurStr); //是新的单词
			WordsNum[d] = 1;
			d++;
		}
	}
	printf("\n打印WordsDistinguished\n");
	PrintWords(WordsDistinct);//打印不重复的单词

	//按从大到小的顺序排序,冒泡排序
	int TmpInt;
	for (int i = 0; i <= MaxWordsNum - 1; i++)
	{
		for (int j = 0; j <= MaxWordsNum - 2; j++)
		{
			if (WordsNum[j] < WordsNum[j + 1])
			{
				//交换
				TmpInt = WordsNum[j];
				WordsNum[j] = WordsNum[j + 1];
				WordsNum[j + 1] = TmpInt;
				char TmpStr1[MaxWordLength] = "";
				strcpy(TmpStr1, WordsDistinct[j + 1]);
				strcpy(WordsDistinct[j + 1], ""); //clear
				strcpy(WordsDistinct[j + 1], WordsDistinct[j]); //copy
				strcpy(WordsDistinct[j], ""); //clear
				strcpy(WordsDistinct[j], TmpStr1); //copy
			}
		}
	}

	printf("The word frequency is described as follows:\n");
	//打印结果
	for (int i = 0; i < 10; i++)
	{
		printf("%s:%d\n", WordsDistinct[i], WordsNum[i]);
	}
}
void PrintWords(char Words[MaxWordsNum][MaxWordLength])
{
	for (int i = 0; i <= MaxWordsNum - 1; i++)
	{
		printf("%s ", Words[i]);
	}
}


